package gui;

import gui.Main.NetIP;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JComboBox;

import java.awt.Component;

import javax.swing.Box;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;

import java.awt.GridBagConstraints;

import javax.swing.JTextField;

import java.awt.Insets;
import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JList;

import server.IDHttpListener;

import java.awt.Color;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AppWindow extends JFrame
{
	private static final long serialVersionUID = -8479666659616396064L;
	private JPanel contentPane;
	private JTextField txtHttp;

	IDHttpListener server = null;
	Main mainApp = null;
	private JComboBox cbNetInterface;
	private JList list;
	private JPanel qrPanel;

	protected static AppWindow instance = null;
	
	/**
	 * Create the frame.
	 */
	public AppWindow(final IDHttpListener server, Main mapp)
	{
		setResizable(false);
		this.server = server;
		this.mainApp = mapp;
		
		setTitle("InstaDir server");
		setIconImage(Toolkit.getDefaultToolkit().getImage(AppWindow.class.getResource("/resources/icons/icon-dark.png")));
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 640, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		JLabel lblNetworkInterface = new JLabel("Network interface : ");
		panel.add(lblNetworkInterface);
		
		cbNetInterface = new JComboBox();
		panel.add(cbNetInterface);
		
		for(NetIP nip : mainApp.myNets)
		{
			cbNetInterface.addItem(nip);
		}
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.WEST);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JLabel lblScanThisQr = new JLabel("Scan this QR code...");
		lblScanThisQr.setHorizontalAlignment(SwingConstants.CENTER);
		lblScanThisQr.setAlignmentX(Component.CENTER_ALIGNMENT);
		panel_1.add(lblScanThisQr, BorderLayout.NORTH);
		
		qrPanel = new QRPanel(null);
		qrPanel.setBackground(Color.WHITE);
		panel_1.add(qrPanel, BorderLayout.CENTER);
		
		Component horizontalStrut = Box.createHorizontalStrut(256);
		panel_1.add(horizontalStrut, BorderLayout.SOUTH);
		
		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.CENTER);
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[]{0, 0, 0};
		gbl_panel_2.rowHeights = new int[]{0, 0, 0, 0, 0};
		gbl_panel_2.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gbl_panel_2.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel_2.setLayout(gbl_panel_2);
		
		JLabel lblorTypeThis = new JLabel("...or type this URL in another device's browser:");
		GridBagConstraints gbc_lblorTypeThis = new GridBagConstraints();
		gbc_lblorTypeThis.insets = new Insets(0, 0, 5, 5);
		gbc_lblorTypeThis.gridx = 0;
		gbc_lblorTypeThis.gridy = 0;
		panel_2.add(lblorTypeThis, gbc_lblorTypeThis);
		
		txtHttp = new JTextField();
		txtHttp.setEditable(false);
		txtHttp.setHorizontalAlignment(SwingConstants.CENTER);
		txtHttp.setFont(new Font("Tahoma", Font.BOLD, 16));
		txtHttp.setText("http://192.168.1.1:15000/");
		GridBagConstraints gbc_txtHttp = new GridBagConstraints();
		gbc_txtHttp.insets = new Insets(0, 0, 5, 5);
		gbc_txtHttp.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtHttp.gridx = 0;
		gbc_txtHttp.gridy = 1;
		panel_2.add(txtHttp, gbc_txtHttp);
		txtHttp.setColumns(10);
		
		JButton btnCp = new JButton("Copy");
		btnCp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StringSelection selection = new StringSelection(txtHttp.getText());
    		    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    		    clipboard.setContents(selection, selection);
			}
		});
		GridBagConstraints gbc_btnCp = new GridBagConstraints();
		gbc_btnCp.insets = new Insets(0, 0, 5, 0);
		gbc_btnCp.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnCp.gridx = 1;
		gbc_btnCp.gridy = 1;
		panel_2.add(btnCp, gbc_btnCp);
		
		JButton btnStopSharing = new JButton("Stop sharing");
		btnStopSharing.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		GridBagConstraints gbc_btnStopSharing = new GridBagConstraints();
		gbc_btnStopSharing.insets = new Insets(0, 0, 5, 5);
		gbc_btnStopSharing.gridx = 0;
		gbc_btnStopSharing.gridy = 2;
		panel_2.add(btnStopSharing, gbc_btnStopSharing);
		
		JButton btnConfig = new JButton("Config");
		btnConfig.setEnabled(false);
		GridBagConstraints gbc_btnConfig = new GridBagConstraints();
		gbc_btnConfig.insets = new Insets(0, 0, 5, 0);
		gbc_btnConfig.gridx = 1;
		gbc_btnConfig.gridy = 2;
		panel_2.add(btnConfig, gbc_btnConfig);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 3;
		panel_2.add(scrollPane, gbc_scrollPane);
		
		list = new JList();
		scrollPane.setViewportView(list);
		list.setModel(new DefaultListModel<String>());
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((DefaultListModel)list.getModel()).clear();
			}
		});
		GridBagConstraints gbc_btnClear = new GridBagConstraints();
		gbc_btnClear.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnClear.anchor = GridBagConstraints.NORTH;
		gbc_btnClear.gridx = 1;
		gbc_btnClear.gridy = 3;
		panel_2.add(btnClear, gbc_btnClear);
		
		cbNetInterface.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				NetIP nip = (NetIP)cbNetInterface.getSelectedItem();
				mainApp.defIFace = nip.ID;
				
				int port = server.getPort();
				
				if(port < 0)
				{
					txtHttp.setText("ERROR");
				}
				else
				{
					String fullURL = "http://" + nip.IP + ":" + port + "/";
					txtHttp.setText(fullURL);
					
					((QRPanel)qrPanel).setImage(Util.generateQR(fullURL));
				}
			}
		});
		
		NetIP nip = (NetIP)cbNetInterface.getSelectedItem();
		mainApp.defIFace = nip.ID;
		
		int port = server.getPort();
		
		if(port < 0)
		{
			txtHttp.setText("ERROR");
		}
		else
		{
			String fullURL = "http://" + nip.IP + ":" + port + "/";
			txtHttp.setText(fullURL);
			
			//TODO generate QR
			((QRPanel)qrPanel).setImage(Util.generateQR(fullURL));
		}
		
		GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		int width = gd.getDisplayMode().getWidth();
		int height = gd.getDisplayMode().getHeight();
		setLocation((width - getWidth()) / 2, (height - getHeight()) / 2);
		
		instance = this;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void addMessage(String msg)
	{
		((DefaultListModel)list.getModel()).addElement(msg);
	}
}
